package cn.edu.xidian.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.Nullable;
import cn.edu.xidian.R;
import cn.edu.xidian.domain.User;
import cn.edu.xidian.untils.IntentCodeUntils;

public class DisplayDetailActivity extends Activity {


    //详细界面的组件
    private TextView tvDetailWebsite;
    private TextView tvDetailUsername;
    private EditText etDetailPwd;

    private Button btnUpdate;
    private Button btnDelete;

    //intent bundle数据对象
    private Intent intent;
    private Bundle bundle;

    //该详细页面的user对象数据
    private User olduser;
    private User newuser;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);


        //获取组件
        tvDetailWebsite = (TextView) findViewById(R.id.tvDetailWebsite);
        tvDetailUsername = (TextView) findViewById(R.id.tvDetailUsername);
        etDetailPwd = (EditText) findViewById(R.id.etDetailPwd);

        btnUpdate = (Button) findViewById(R.id.btnUpdate);
        btnDelete = (Button) findViewById(R.id.btnDelete);

        //获取从bundle来的对象
        intent = getIntent();
        bundle = intent.getExtras();

        olduser = bundle.getParcelable("olduser");
        bundle.putParcelable("olduser",olduser);
        bundle.putParcelable("newuser",null);


        //设置页面的内容
        tvDetailWebsite.setText(olduser.getUser_website());
        tvDetailUsername.setText(olduser.getUser_name());
        etDetailPwd.setText(olduser.getUser_pwd());

        btnUpdate.setOnClickListener(ls);
        btnDelete.setOnClickListener(ls);

        intent.putExtras(bundle);

        //设置返回码
        setResult(IntentCodeUntils.RESULT_DETAIL,intent);


    }

    /**
     * 更新，删除按钮监听
     */
    View.OnClickListener ls = new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            switch(v.getId()){
                case R.id.btnUpdate:
                    newuser = new User(
                            olduser.getUser_id(),
                            olduser.getUser_website(),
                            olduser.getUser_name(),
                            etDetailPwd.getText().toString()
                    );
                    bundle.putParcelable("newuser",newuser);
                    intent.putExtras(bundle);
                    Toast.makeText(DisplayDetailActivity.this,"更新成功",Toast.LENGTH_SHORT).show();
                    finish();
                    break;

                case R.id.btnDelete:
                    newuser = new User(olduser.getUser_id(), "delete","delete","delete");
                    bundle.putParcelable("newuser",newuser);
                    intent.putExtras(bundle);
                    Toast.makeText(DisplayDetailActivity.this,"删除成功",Toast.LENGTH_SHORT).show();
                    finish();
                    break;
            }
        }
    };
}
